﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace KD.Sms.Monitor
{
    public partial class frmProtect : Form
    {
        bool _bIsBusy = false;
        public frmProtect()
        {
            InitializeComponent();
        }

        private void frmProtect_Load(object sender, EventArgs e)
        {
            Task.Cyj.Instance.PlcDataReadOut += OnPlcDataReadOut;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="send"></param>
        /// <param name="e"></param>
        private void OnPlcDataReadOut(object send, Task.ActiveReadEventArgs e)
        {
            if (_bIsBusy)
                return;
            try
            {
                _bIsBusy = true;
                short[] _arrReads = (short[])e.E_ReadValue;
                int[] _aryBits360 = DataConvert.Ushort2Binary((int)(_arrReads[60 / 2]));
                int[] _aryBits362 = DataConvert.Ushort2Binary((int)(_arrReads[62 / 2]));
                int[] _aryBits334 = DataConvert.Ushort2Binary((int)(_arrReads[34 / 2]));//司机采样
                kdOpenColse1.C_Value = _aryBits360[0] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse2.C_Value = _aryBits360[1] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse3.C_Value = _aryBits360[2] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse4.C_Value = _aryBits360[3] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse5.C_Value = _aryBits360[4] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse12.C_Value = _aryBits360[5] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse11.C_Value = _aryBits360[6] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse10.C_Value = _aryBits360[7] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse9.C_Value = _aryBits360[8] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse18.C_Value = _aryBits360[9] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse6.C_Value = _aryBits360[10] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse17.C_Value = _aryBits360[11] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse16.C_Value = _aryBits360[12] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse15.C_Value = _aryBits360[13] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse24.C_Value = _aryBits360[14] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse23.C_Value = _aryBits360[15] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse25.C_Value = _aryBits334[6] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;

                kdOpenColse8.C_Value = _aryBits360[0] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse20.C_Value = _aryBits360[1] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse7.C_Value = _aryBits360[2] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse14.C_Value = _aryBits360[3] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse22.C_Value = _aryBits360[5] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
                kdOpenColse21.C_Value = _aryBits360[6] == 1 ? UI.TwoStateValue.On : UI.TwoStateValue.Off;
            }
            catch (Exception ex)
            {
                FormHelper.ApplicationShow(UI.MsgType.Error, "限位状态解析出错!详见日志!");
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Forms, "限位状态解析出错!");
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Forms, ex.ToString());
            }
            finally
            {
                _bIsBusy = false;
            }
        }
    }
}
